package yaohua.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.jsoup.nodes.Document;

import yaohua.entity.Entity_help;
import yaohua.jsoup.Jsoup_help;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

/**
 * 过期查询
 * 
 * @author yh
 * 
 */
public class ExtendedActivity extends ListActivity
	{
		List<String> list = null;// 在刷新代码中的书籍List
		String result = null;// 在按钮监听代码得到网页源代码
		ProgressDialog progressDialog;

		public static final String EXTENDED = "http://202.207.182.100:8080/pft/dzjs/cqcx.asp";
		private Button button;

		@Override
		protected void onCreate(Bundle savedInstanceState)
			{
				super.onCreate(savedInstanceState);
				setContentView(R.layout.tab);
				button = (Button) findViewById(R.id.refresh);
				button.setOnClickListener(new ExtendedListener());
			}

		@Override
		protected void onListItemClick(ListView l, View v, int position, long id)
			{
				super.onListItemClick(l, v, position, id);
				new Jsoup_help().handlerItem(result, "tdborder2", 6, position, (ArrayList<String>) list, this, ItemActivity.class);
			}

		@Override
		public boolean onOptionsItemSelected(MenuItem item)
			{
				new AlertDialog.Builder(this).setTitle("关于").setMessage("本程序为中北大学图书馆使用,欢迎您能向作者提供在使用过程中的bug,以方便作者进行修改.联系作者新浪微薄@耀华info")
						.show();
				return super.onOptionsItemSelected(item);
			}

		@Override
		public boolean onCreateOptionsMenu(Menu menu)
			{
				menu.add("关于");
				return super.onCreateOptionsMenu(menu);
			}

		class ExtendedListener implements OnClickListener
			{

				public void onClick(View v)
					{
						progressDialog = ProgressDialog.show(ExtendedActivity.this, "正在查询", "请稍等...", false, true);
						// 在新的线程处理数据
						new Thread(new Runnable()
							{

								public void run()
									{
										try
											{
												Entity_help entity_help = new Entity_help();
												HttpResponse httpResponse = entity_help.getResponse(EXTENDED);
												entity_help.handleBook(httpResponse, handler, "tdborder4", 1, 6, progressDialog);
											}
										catch (Exception e)
											{
												e.printStackTrace();
											}
										finally
											{
												progressDialog.dismiss();
											}
									}

							}).start();
					}

				// 配合线程处理UI
				Handler handler = new Handler()
					{

						@Override
						public void handleMessage(Message msg)
							{
								super.handleMessage(msg);
								if (msg.arg1 == -1)
									{
										progressDialog.dismiss();
										Toast.makeText(getApplicationContext(), "网络连接不可用，请检查网络设置", Toast.LENGTH_SHORT).show();
									}
								else
									{
										Bundle bundle = msg.getData();
										ArrayList<String> books = bundle.getStringArrayList("books");
										list = bundle.getStringArrayList("list");
										result = bundle.getString("result");
										if (books.size() == 0)
											{
												Toast.makeText(getApplicationContext(), "没有任何记录", Toast.LENGTH_SHORT).show();
											}
										// 初始化listActivity的数据
										else
											{
												ArrayAdapter<String> ad;
												ad = new ArrayAdapter<String>(ExtendedActivity.this,
														android.R.layout.simple_list_item_1, (String[]) books.toArray(new String[books
																.size()]));
												// 这里无法直接调用,必须在主线程才可以
												setListAdapter(ad);
											}

									}

							}

					};
			}

	}
